package test;// 使用 @SelectProvider 实现动态SQL
@SelectProvider(type = UserSqlProvider.class, method = "selectByCondition")
List<User> selectByCondition(UserCondition condition);

// 参数类
@Data
public class UserCondition {
    private String username;
    private String email;
    private Date startCreateTime;
    private Date endCreateTime;
}

// SQL 提供类
public class UserSqlProvider {
    public String selectByCondition(UserCondition condition) {
        return new SQL() {{
            SELECT("*");
            FROM("user");
            if (condition.getUsername() != null) {
                WHERE("username LIKE CONCAT('%',#{username},'%')");
            }
            if (condition.getEmail() != null) {
                WHERE("email LIKE CONCAT('%',#{email},'%')");
            }
            if (condition.getStartCreateTime() != null) {
                WHERE("create_time >= #{startCreateTime}");
            }
            if (condition.getEndCreateTime() != null) {
                WHERE("create_time <= #{endCreateTime}");
            }
        }}.toString();
    }
}